<?php
// +----------------------------------------------------------------------
// | Demo [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lmx0536.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: limx <715557344@qq.com> <http://www.lmx0536.cn>
// +----------------------------------------------------------------------
// | Date: 2016/4/26 Time: 20:06
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;

/**
 * 模型模版
 */

class CateModel extends Model {
	//表名
	protected $tableName = 'category';

	/**
	 * 文件模型自动完成
	 * @var array
	 */
	protected $_auto = array(
	    array('update_time', 'fnGetNowTime', self::MODEL_BOTH, 'function'),
	);

	protected $_validate = array(
	    //array('verify', 'require', '验证码必须！'), //默认情况下用正则进行验证
	    //array('name', '', '帐号名称已经存在！', 0, 'unique', 1), // 在新增的时候验证name字段是否唯一
	    //array('value', array(1, 2, 3), '值的范围不正确！', 2, 'in'), // 当值不为空的时候判断是否在一个范围内
	    //array('repassword', 'password', '确认密码不正确', 0, 'confirm'), // 验证确认密码是否和密码一致
	    //array('password', 'checkPwd', '密码格式不正确', 0, 'function'), // 自定义函数验证密码格式
	);

	/**
	 * 删除
	 */
	public function mfnDelete($id) {

        $condition["Id"] = $id;
        $res = $this -> where($condition) -> delete();
        if ($res) {
            return true;
        }
		$this -> error="删除失败！";
		return false;

	}

	/**
	 * [mfnAdd 添加、编辑]
	 * @Author   Limx
	 * @Method   直接调用
	 * @DateTime 2016-02-02T09:54:54+0800
	 * @param    [type]                   $d    [所需的数据]
	 * @return   [type]                         [description]
	 */
	public function mfnUpdate($d) {
		$data = $this -> create($d);

		if (empty($data)) {
		    $this -> error = '数据不能为空！';
			return false;
		}

		if (empty($data['Id'])) {
			$id = $this -> add();
			if (!$id) {
				$this -> error = '新增出错！';
				return false;
			}
		} else {
			$status = $this -> save();
			if (false === $status) {
				$this -> error = '更新出错！';
				return false;
			}
			$id = $data['Id'];
		}

		return $id;
	}

	/**
	 * [mfnFind 获取一条信息]
	 * @Author   Limx
	 * @Method   直接调用
	 * @DateTime 2016-02-02T09:54:54+0800
	 * @param    [type]              $condition [筛选条件]
	 * @return   [type]                         [description]
	 */
	public function mfnFind($condition, $field = "", $lock = false) {
		$res = $this -> lock($lock) -> field($field) -> where($condition) -> find();

		return $res;
	}

	/**
	 * [mfnSelect 获取列表]
	 * @Author   Limx
	 * @Method   直接调用
	 * @DateTime 2016-02-02T09:54:54+0800
	 * @param    [type]              $condition [筛选条件]
	 * @return   [type]                         [description]
	 */
	public function mfnSelect($condition, $pageIndex = 0, $pageSize = 10, $order = "Id desc", $field = "", $lock = false) {
		$start = $pageIndex * $pageSize;
		$result = $this -> field($field) 
		        -> lock($lock)
				-> where($condition) 
				-> order($order) 
				-> limit("$start,$pageSize") 
				-> select();
		$count = $this -> where($condition) -> count(); 
		$returnData["data"]=$result;
		$returnData["count"]=$count;
		return $returnData;
	}

}
